<?
class Product extends DB
{
	function __construct($other_conn=null)
	{
		parent::__construct(Tproduct,$other_conn);
	}
	function add_sold_quantity_by_order($ref_no)
	{
		$sales_order=new DB(Tsales_order);
		$sales_order_detail=new DB(Tsales_order_detail);
		$product_variation=new DB(Tproduct_variation);
		$tem = $sales_order->get_row($ref_no,'ref_no');
		$saler_order_id=$tem['id'];
		$tem = $sales_order_detail->get_rows(1024,'sales_order_id',$saler_order_id);
		foreach($tem as $val){
			$this->add('sold_quantity',$val['quantity'],'id',$val['product_id']);
		}
	}
	function reduce_stock_by_order($ref_no)
	{
		$sales_order=new DB(Tsales_order);
		$sales_order_detail=new DB(Tsales_order_detail);
		$product_variation=new DB(Tproduct_variation);
		$tem = $sales_order->get_row($ref_no,'ref_no');
		$saler_order_id=$tem['id'];
		$tem = $sales_order_detail->get_rows(1024,'sales_order_id',$saler_order_id);
		foreach($tem as $val){
			if($val['product_variation_id']>0)
			$product_variation->reduce('stock',$val['quantity'],'id',$val['product_variation_id']);
			else $this->reduce('stock',$val['quantity'],'id',$val['product_id']);
		}
	}
	function restore_stock_by_order($ref_no)
	{
		$sales_order=new DB(Tsales_order);
		$sales_order_detail=new DB(Tsales_order_detail);
		$product_variation=new DB(Tproduct_variation);
		$tem = $sales_order->get_row($ref_no,'ref_no');
		$saler_order_id=$tem['id'];
		$tem = $sales_order_detail->get_rows(1024,'sales_order_id',$saler_order_id);
		foreach($tem as $val){
			if($val['product_variation_id']>0)
			$product_variation->add('stock',$val['quantity'],'id',$val['product_variation_id']);
			else $this->add('stock',$val['quantity'],'id',$val['product_id']);
		}
	}
	function get_left($id, $where='', $value='', $lang=0){
		$wherer='';
		$params=array();
		$params[':id']=$id;
		if( ! empty($where) ) { $wherer=" $where=:value and "; $params[':value']=$value; }
		if($lang!=0) $wherer.=" language_id=$lang and ";
		$wherer.=" is_enabled=1 and ";
		$sql="SELECT * FROM $this->table where $wherer id < :id order by id desc limit 1 ";
		$stmt = $this->prepare($sql);
		$stmt->execute($params);
		if($stmt->rowCount()==0) return 0;
		$result = $stmt->fetchAll(PDO::FETCH_BOTH);
		return $result[0];
	}
	function get_right($id, $where='', $value='', $lang=0){
		$wherer='';
		$params=array();
		$params[':id']=$id;
		if( ! empty($where) ) { $wherer=" $where=:value and "; $params[':value']=$value; }
		if($lang!=0) $wherer.=" language_id=$lang and ";
		$wherer.=" is_enabled=1 and ";
		$sql="SELECT * FROM $this->table where $wherer id > :id order by id asc limit 1 ";
		$stmt = $this->prepare($sql);
		$stmt->execute($params);
		if($stmt->rowCount()==0) return 0;
		$result = $stmt->fetchAll(PDO::FETCH_BOTH);
		return $result[0];
	}
}
?>
